package com.jiangruanjavaproject.service;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.MapSerializer;
import com.jiangruanjavaproject.Constants;
import com.jiangruanjavaproject.request.HttpUtils;

import java.sql.Connection;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @author:wanfeng
 * @desc: 该类用于调用比特币pc功能, 获取最新区块哈希值
 * @datetime:2021/12/19 15:56
 */
public class GetBestBlockHashService {
    public String getBestBlockHash(){
        //准备用户信息
        String author = Constants.BITCOIN_RPC_USER + ":"+  Constants.BITCOIN_RPC_PWD;
        Base64.Encoder encoder = Base64.getEncoder();
        String authorBase64 = encoder.encodeToString(author.getBytes());

        //准备请求头信息
        Map<String,String> header = new HashMap<>();
        header.put("ContentType","application/json");
        header.put("Authorization","Basic "+ authorBase64);
        //准备请求的数据
        Map<String,Object> body = new HashMap<>();
        body.put("id",1001);
        body.put("method","getbestblockhash");
        body.put("params",null);
        body.put("version","2.0");
        //将map转换为byte[]
        byte[] bodyBytes = JSONObject.toJSONBytes(body);
     // {"id":1001,"result":"xxxxx"， "error":null}

        String result = HttpUtils.post(Constants.BITCOIN_RPC_URL,header,bodyBytes);

       // HttpUtils.post(Constants.BITCOIN_RPC_URL,header,bodyBytes);
    //对resultj进行json反序列化
        return  result;
    }
}
